package com.scribbles.admin.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.scribbles.admin.service.LookupService;
import com.scribbles.common.dto.LookupTypeDTO;
import com.scribbles.common.entity.LookupType;

@Controller
@RequestMapping(value = "dropdown")
public class LookupController {
	private final Logger logger = Logger.getLogger(LookupController.class);

	@Autowired
	private LookupService lookupService;

	/**
	 * Method load.
	 * @return String
	 */
	@RequestMapping(value = "load")
	public String load() {
		return "admin/dropdown";
	}

	/**
	 * Method retrieveAllLookupTypes.
	 * @param modelMap Map<String,Object>
	 * @return Map<String,Object>
	 */
	@RequestMapping(value = "retrieveAll", method = RequestMethod.GET)
	public Map<String, Object> retrieveAllLookupTypes(
			Map<String, Object> modelMap) {
		// logger.info("Loading Lookuptype Grid data ..." + lookuptypeId);
		List<LookupType> lookupTypes = lookupService.retrieveLookupTypes();
		List<LookupTypeDTO> lookupTypeDTOs = convertLookupTypesEntitiesToDTOs(lookupTypes);
		logger.info("Loading all lookupTypes : " + lookupTypes.size());
		modelMap.put("lookupTypes", lookupTypeDTOs);
		modelMap.put("total", lookupTypeDTOs.size());
		modelMap.put("success", true);
		logger.info("Completed Loading LookupType Grid data in the controller");
		return modelMap;
	}

/*
 * 	This method is used to test the JPA 2 features. 
 * 
 * 	@RequestMapping(value = "retrieveAllUsingCriteria", method = RequestMethod.GET)
	public Map<String, Object> retrieveAllLookupTypesUsingCriteria(
			Map<String, Object> modelMap) {
		// logger.info("Loading Lookuptype Grid data ..." + lookuptypeId);
		List<Object> lookupTypes = lookupService
				.testCriteriaAPIretrieveLookupTypes();
		logger.info("Loading all lookupTypes : " + lookupTypes.size());
		modelMap.put("lookupTypes", lookupTypes);
		modelMap.put("total", lookupTypes.size());
		modelMap.put("success", true);
		logger.info("Completed Loading LookupType Grid data in the controller");
		return modelMap;
	}*/

	/**
 * Method save.
 * @param lookupTypes Object
 * @param modelMap Map<String,Object>
 * @return Map<String,Object>
 */
@RequestMapping(value = "save", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> save(@RequestParam("lookupTypes") Object lookupTypes,
			Map<String, Object> modelMap) {
		// logger.info("lookupTypeId ..." + lookupTypeId);
		logger.info("lookupType ..." + lookupTypes);

		modelMap.put("success", true);
		logger.info("Completed saving lookup type");
		return modelMap;
	}
	
	/**
	 * Method convertLookupTypeEntityToDTO.
	 * @param lookupType LookupType
	 * @return LookupTypeDTO
	 */
	private LookupTypeDTO convertLookupTypeEntityToDTO(LookupType lookupType) {
		LookupTypeDTO lookupTypeDTO = new LookupTypeDTO();
		lookupTypeDTO.setId(lookupType.getId());
		lookupTypeDTO.setName(lookupType.getName());
		lookupTypeDTO.setVersion(lookupType.getVersion());
		lookupTypeDTO.setCreatedBy(lookupType.getCreatedBy());
		lookupTypeDTO.setUpdatedBy(lookupType.getUpdatedBy());
		return lookupTypeDTO;
	}
	/**
	 * Method convertLookupTypesEntitiesToDTOs.
	 * @param lookupTypes List<LookupType>
	 * @return List<LookupTypeDTO>
	 */
	private List<LookupTypeDTO> convertLookupTypesEntitiesToDTOs(List<LookupType> lookupTypes) {
		List<LookupTypeDTO> lookupTypeDTOs = new ArrayList<LookupTypeDTO>();
		for (LookupType lookupType : lookupTypes) {
			lookupTypeDTOs.add(convertLookupTypeEntityToDTO(lookupType));
		}
		return lookupTypeDTOs;
	}
}
